<?php
/** 
 * User_model
 * This class handles model work necessary for User Crud and List operations 
 * 
 * @author Ray Nowell
 *	
 */ 
class User_model extends MY_Model {

	function User_model()
	{
		parent::MY_Model();
	}
	
	function Add_user($data)
	{
		return $this->db->insert('users', $data);
	}
	
	function Update_user($data)
	{				
		$this->db->where('id',element('id', $data));
		
		return $this->db->update('users',$data);
	}
	
	function Delete_user()
	{
		$ret_val = $this->delete_record('users','id');
		return $ret_val;	
	}
	/*
	 * Returns a list of current users
	 * @todo must filter by logged in user
	 */
	function get_user_list()
	{
		$select_fields	=	"users.id
							,username
							,first_name
							,last_name
							,email
							,groups.name
							,facility_name";
										 //,CASE active WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' ELSE '' END AS Active";
		//Get the query results for the clinical users
		$this->db->select($select_fields);
		$this->db->from('users');
		$this->db->join('meta', 'meta.user_id = users.id', 'left');
		$this->db->join('facility', 'facility.facility_id = meta.current_facility_id','left');
		$this->db->join('groups', 'groups.id = users.group_id','left');
		$this->db->order_by('username');
		$query = $this->db->get();
				
		return $query->result_array();
	}
	
	function get_user_group_lookup()
	{
		$this->db->select('id, name');
		$this->db->order_by('name');
		$query = $this->db->get('groups');
		
		return $query->result_array(); 
	}
	
	function get_facility_lookup()
	{
		$this->db->select('facility_id, facility_name');
		$this->db->order_by('facility_name');
		$query = $this->db->get('facility');
		
		return $query->result_array(); 
	}

	/*
	 * Returns a single user record based on Cliincal User Id passed 
	 */
	function get_user_record($user_id)
	{
		$query = $this->db->limit(1)->get_where('users', array('user_id' => $id)); 
		return $query->row();
	}
	/*
	 * Returns a single user record based on Cliincal Pharmacist Id passed 
	 */
	function get_user_id_record($user_id)
	{
		
		$this->db->from('users');
		$this->db->join('meta', 'meta.user_id = users.id', 'left');
		$this->db->join('facility', 'facility.facility_id = meta.current_facility_id','left');
		$this->db->join('groups', 'groups.id = users.group_id','left');
		$this->db->where('users.id',$user_id);
		$query = $this->db->limit(1)->get();
		return $query->row();
	}
}

/* End of file user_model.php */
/* Location: ./system/application/models/user_model.php */